package com.igeek;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //处理字符集
        //父-》子
        HttpServletRequest  request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //设置字符集
        request.setCharacterEncoding("utf-8");//POST请求有效
        response.setContentType("text/html;charset=utf-8");
        //执行后续内容
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
